// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 

package com.xy.core.dao.impl;

import com.xy.core.command.*;
import com.xy.core.dao.DynamicDao;
import com.xy.core.web.paging.PagingBean;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.*;
import org.hibernate.*;
import org.hibernate.criterion.Projections;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.hql.ast.QueryTranslatorImpl;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class DynamicDaoImpl
    implements DynamicDao
{

    public DynamicDaoImpl(String s)
    {
        entityClassName = s;
    }

    public DynamicDaoImpl()
    {
    }

    public String getEntityClassName()
    {
        return entityClassName;
    }

    public void setEntityClassName(String s)
    {
        entityClassName = s;
    }

    public SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionfactory)
    {
        sessionFactory = sessionfactory;
    }

    public HibernateTemplate getHibernateTemplate()
    {
        if(hibernateTemplate == null)
            hibernateTemplate = new HibernateTemplate(sessionFactory);
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernatetemplate)
    {
        hibernateTemplate = hibernatetemplate;
    }

    public Object save(Object obj)
    {
        getHibernateTemplate().save(entityClassName, obj);
        return obj;
    }

    public Object merge(Object obj)
    {
        getHibernateTemplate().merge(entityClassName, obj);
        return obj;
    }

    public Object get(Serializable serializable)
    {
        return getHibernateTemplate().load(entityClassName, serializable);
    }

    public void remove(Serializable serializable)
    {
        getHibernateTemplate().delete(entityClassName, get(serializable));
    }

    public void remove(Object obj)
    {
        getHibernateTemplate().delete(entityClassName, obj);
    }

    public void evict(Object obj)
    {
        getHibernateTemplate().evict(obj);
    }

    public Long getTotalItems(String s, final Object values[])
    {
        int i = s.toUpperCase().indexOf(" ORDER BY ");
        if(i != -1)
            s = s.substring(0, i);
        QueryTranslatorImpl querytranslatorimpl = new QueryTranslatorImpl(s, s, Collections.EMPTY_MAP, (SessionFactoryImplementor)getSessionFactory());
        querytranslatorimpl.compile(Collections.EMPTY_MAP, false);
        final String sql = (new StringBuilder()).append("select count(1) from (").append(querytranslatorimpl.getSQLString()).append(") tmp_count_t").toString();
        Object obj = getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session session)
                throws HibernateException, SQLException
            {
                SQLQuery sqlquery = session.createSQLQuery(sql);
                if(values != null)
                {
                    for(int j = 0; j < values.length; j++)
                        sqlquery.setParameter(j, values[j]);

                }
                return sqlquery.uniqueResult();
            }

          
        }
);
        return new Long(obj.toString());
    }

    public List getAll()
    {
        return (List)getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session session)
                throws HibernateException, SQLException
            {
                String s = (new StringBuilder()).append("from ").append(entityClassName).toString();
                Query query = session.createQuery(s);
                return query.list();
            }

         
        }
);
    }

    public List getAll(final PagingBean pb)
    {
        final String hql = (new StringBuilder()).append("from ").append(entityClassName).toString();
        int i = getTotalItems(hql, null).intValue();
        pb.setTotalItems(i);
        return (List)getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session session)
                throws HibernateException, SQLException
            {
                Query query = session.createQuery(hql);
                query.setFirstResult(pb.getFirstResult()).setFetchSize(pb.getPageSize().intValue());
                query.setMaxResults(pb.getPageSize().intValue());
                return query.list();
            }

           
        }
);
    }

    public List getAll(final QueryFilter queryFilter)
    {
        int i = getCountByFilter(queryFilter);
        queryFilter.getPagingBean().setTotalItems(i);
        List list = (List)getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session session)
                throws HibernateException, SQLException
            {
                Criteria criteria = session.createCriteria(entityClassName);
                queryFilter.getAliasSet().clear();
                setCriteriaByQueryFilter(criteria, queryFilter);
                return criteria.list();
            }

         
        }
);
        return list;
    }

    protected int getCountByFilter(final QueryFilter filter)
    {
        Object obj = getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session session)
                throws HibernateException, SQLException
            {
                Criteria criteria = session.createCriteria(entityClassName);
                for(int i = 0; i < filter.getCommands().size(); i++)
                {
                    CriteriaCommand criteriacommand = (CriteriaCommand)filter.getCommands().get(i);
                    if(!(criteriacommand instanceof SortCommandImpl))
                        criteria = criteriacommand.execute(criteria);
                }

                criteria.setProjection(Projections.rowCount());
                return criteria.uniqueResult();
            }
 
        }
);
        if(obj == null)
            return (new Integer(0)).intValue();
        else
            return (new Integer(obj.toString())).intValue();
    }

    private Criteria setCriteriaByQueryFilter(Criteria criteria, QueryFilter queryfilter)
    {
        for(int i = 0; i < queryfilter.getCommands().size(); i++)
            criteria = ((CriteriaCommand)queryfilter.getCommands().get(i)).execute(criteria);

        criteria.setFirstResult(queryfilter.getPagingBean().getFirstResult());
        criteria.setMaxResults(queryfilter.getPagingBean().getPageSize().intValue());
        return criteria;
    }

    private String entityClassName;
    private SessionFactory sessionFactory;
    private HibernateTemplate hibernateTemplate;


}
